package com.ruoyi.platform.system.user.manager;

import com.ruoyi.base.enums.ExceptionEnum;
import com.ruoyi.base.exception.SysException;
import com.ruoyi.base.service.BaseService;
import com.ruoyi.platform.system.role.dao.UserRoleDAO;
import com.ruoyi.platform.system.role.enums.RoleEnum;
import com.ruoyi.platform.system.role.pojo.UserRoleDO;
import com.ruoyi.platform.system.user.dao.UserDAO;
import com.ruoyi.platform.system.user.pojo.UserDO;
import com.ruoyi.platform.system.user.pojo.UserDTO;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * 用户
 *
 * @author lcf
 */
@Component
public class UserManager extends BaseService {
    @Autowired
    private UserDAO userDAO;
    @Autowired
    private UserRoleDAO userRoleDAO;

    /**
     * 检查是否是系统用户
     */
    public void checkSystemUser(long userId) {
        if (isSystemUser(userId)) {
            throw new SysException("不允许操作管理员");
        }
    }

    /**
     * 获取是否系统用户
     */
    public boolean isSystemUser(long userId) {
        UserDO user = getUser(userId);
        UserRoleDO userRole = userRoleDAO.getUserRole(userId, RoleEnum.MANAGER.getCode());
        return user != null && userRole != null;
    }

    /**
     * 根据用户id获取用户列表
     */
    public List<UserDTO> selectByUserIdList(List<Long> userIdList) {
        if (CollectionUtils.isEmpty(userIdList)) {
            return Collections.emptyList();
        }

        List<UserDO> userList = userDAO.selectUserByPrimaryKeyList(userIdList);
        if (CollectionUtils.isEmpty(userList)) {
            return Collections.emptyList();
        }

        return userList.stream().map(UserDTO::new).collect(Collectors.toList());
    }

    /**
     * 检查用户id
     */
    public UserDO checkUserId(long userId) {
        return getOrThrow(() -> userDAO.selectUserByPrimaryKey(userId), ExceptionEnum.USER_NOT_EXIST);
    }

    /**
     * 获取用户
     */
    public UserDO getUser(long userId) {
        return userDAO.selectUserByPrimaryKey(userId);
    }

    /**
     * 检查账号是否存在
     */
    public void checkAccountExist(Long userId, String account) {
        if (StringUtils.isNotBlank(account)) {
            UserDO user = userDAO.getByAccount(account);
            if (user != null && !Objects.equals(userId, user.getUserId())) {
                throw exception(ExceptionEnum.ACCOUNT_EXIST);
            }
        }
    }

    /**
     * 检查手机号码是否存在
     */
    public void checkPhoneExist(Long userId, String phone) {
        if (StringUtils.isNotBlank(phone)) {
            UserDO user = userDAO.getByPhone(phone);
            if (user != null && !Objects.equals(userId, user.getUserId())) {
                throw exception(ExceptionEnum.PHONE_EXISTS);
            }
        }
    }

    /**
     * 检查手机号
     */
    public UserDO checkPhone(String phone) {
        if (StringUtils.isBlank(phone)) {
            throw exception("手机号为空");
        }

        return Optional.ofNullable(userDAO.getByPhone(phone)).orElseThrow(() -> exception(ExceptionEnum.PHONE_NOT_EXISTS));
    }
}
